
********************************************************************************
$ontext
This is the numerical bottom-up optimization model used in
St�ckl, F. and A. Zerrahn (2020): Substituting Clean for Dirty Energy: A Bottom-Up Analysis

This model is derived from
The Dispatch and Investment Evaluation Tool with Endogenous Renewables (DIETER).
Version 1.3.0, November 2018.
Written by Alexander Zerrahn and Wolf-Peter Schill.

This work is licensed under the MIT License (MIT).
For more information on this license, visit http://opensource.org/licenses/mit-license.php.
Whenever you use this code, please refer to http://www.diw.de/dieter.
We are happy to receive feedback under azerrahn@diw.de and wschill@diw.de.
$offtext
********************************************************************************




***************  SETS  *********************************************************

***** Sets used in the model *****
Sets
tech                     Generation technologies
 dis(tech)               Dispatchable generation technologies
 nondis(tech)            Nondispatchable generation technologies
 con(tech)               Conventional generation technologies
 res(tech)               Renewable generation technologies
sto                      Storage technologies
year                     Base year for temporal data
h                        Hours
n                        Nodes



***** Sets used for data upload *****
headers_tech                     Generation technologies - upload headers
 tech_dispatch                   Generation technologies - dispatchable or nondispatchable
 tech_res_con                    Generation technologies - renewable or "conventional"
headers_sto                      Storage technologies - upload headers

headers_time                     Temporal data - upload headers
;




***************  PARAMETERS  ***************************************************

Parameters

***** Generation technologies *****
*--- Variable and fixed costs ---*
eta              Efficiency of conventional technologies [0 1]
carbon_content   CO2 emissions per fuel unit used [tons per MWh thermal]
kappa_fix        Annual fixed costs [EUR per MW per year]
CO2price         CO2 price in [EUR per ton]

*--- Investment ---*
kappa_inv_overnight      Investment costs: Overnight [EUR per MW]
lifetime                 Investment costs: technical lifetime [a]
interest_rate            Investment costs: Interest rate [%]
nu_p                     Investment: maximum installable capacity per technology [MW]
nu_e                     Investment: maximum installable energy [TWh per a]


***** Fuel and CO2 costs *****
fuelprice        Fuel price conventionals [EUR per MWh thermal]


***** Renewables *****
iota             Minimum renewables share [0 1]


***** Storage *****
*--- Variable and fixed costs ---*
kappa_v_sto_in           Marginal costs of storing in  [EUR per MWh]
kappa_v_sto_out          Marginal costs of storing out [EUR per MWh]
eta_sto_in               Storage efficiency [0 1]
eta_sto_out              Storage efficiency [0 1]
phi_sto_ini              Initial storage level [0 1]
tau_sto                  Maximum E to P ratio of storage types [#]
kappa_fix_sto_p_in       Annual fixed costs [EUR per MW]
kappa_fix_sto_p_out      Annual fixed costs [EUR per MW]
kappa_fix_sto_e          Annual fixed costs [EUR per MW]

*--- Investment ---*
kappa_inv_overnight_sto_e        Investment costs for storage energy: Overnight [EUR per MWh]
kappa_inv_overnight_sto_p_in     Investment costs for storage capacity: Overnight [EUR per MW]
kappa_inv_overnight_sto_p_out    Investment costs for storage capacity: Overnight [EUR per MW]
nu_sto_e                         Investment into storage: maximum installable energy [MWh]
nu_sto_p                         Investment into storage: maximum installable power [MW]


***** Time Data *****
delta_y                  Demand hour h for different years [MWh]
delta                    Demand hour h [MWh]
phi_res_y                Renewables availability technology res in hour h for different years [0 1]
phi_res                  Renewables availability technology res in hour h [0 1]
phi_ror_y                Run-of-river availability technology ror in hour h for different years [0 1]
phi_ror                  Run-of-river availability technology ror in hour h [0 1]


***************  DERIVED PARAMETERS  *******************************************

kappa_v              Marginal production costs for conventional plants [EUR per MWh]
kappa_i              Annualized investment costs by conventioanl plant [EUR per MW]

kappa_i_res          Annualized investment costs by renewable plant [EUR per MW]
kappa_fix_res        Annualized fixed costs by renewable plant [EUR per MW per a]

kappa_i_sto_e        Annualized investment costs storage energy [EUR per MWh]
kappa_i_sto_p_in     Annualized investment costs storage capacity [EUR per MW]
kappa_i_sto_p_out    Annualized investment costs storage capacity [EUR per MW]



***************  PARAMETERS FOR DATA UPLOAD  ***********************************

technology_data_upload(n,tech,tech_res_con,tech_dispatch,headers_tech)
technology_data(n,tech,headers_tech)
storage_data(n,sto,headers_sto)

time_data_upload(h,n,year,headers_time)
;




***************  UPLOAD TIME-CONSTANT SETS AND PARAMETERS  *********************

$onecho >temp.tmp
se=0

dset=n                                   rng=spatial!B2                  rdim=0 cdim=1

dset=tech                                rng=Technologies!B6             rdim=1 cdim=0
dset=headers_tech                        rng=Technologies!E5             rdim=0 cdim=1
dset=tech_dispatch                       rng=Technologies!D6             rdim=1 cdim=0
dset=tech_res_con                        rng=Technologies!C6             rdim=1 cdim=0

dset=sto                                 rng=storage!B6                  rdim=1 cdim=0
dset=headers_sto                         rng=storage!C5                  rdim=0 cdim=1

par=technology_data_upload       rng=Technologies!A5     rdim=4 cdim=1
par=storage_data                 rng=storage!A5          rdim=2 cdim=1

$offecho

%skip_Excel%$call "gdxxrw data_input.xlsx @temp.tmp o=Data_input maxdupeerrors=100";

$GDXin Data_input.gdx
$load n tech headers_tech tech_dispatch tech_res_con
$load sto headers_sto
$load technology_data_upload storage_data
;



***************  UPLOAD TIME-SERIES SETS AND PARAMETERS  ***********************

$onecho >temp2.tmp
se=0

dset=h                           rng=basic!A9            rdim=1 cdim=0
dset=headers_time                rng=basic!B8            rdim=0 cdim=1
dset=year                        rng=basic!B7            rdim=0 cdim=1
par=time_data_upload             rng=basic!A6            rdim=1 cdim=3
$offecho

%skip_Excel%$call "gdxxrw time_series.xlsx @temp2.tmp o=time_series";

$GDXin time_series.gdx
$load h headers_time year time_data_upload
;




***************  ASSIGNMENTS  **************************************************



***** Aliases *****
alias (h,hh) ;
alias (res,resres) ;
alias (nondis,nondisnondis) ;



***** Derived sets *****
dis(tech)$sum( (n,tech_res_con,headers_tech), technology_data_upload(n,tech,tech_res_con,'dis',headers_tech)) = yes;
nondis(tech)$sum( (n,tech_res_con,headers_tech), technology_data_upload(n,tech,tech_res_con,'nondis',headers_tech)) = yes;

con(tech)$sum( (n,tech_dispatch,headers_tech), technology_data_upload(n,tech,'con',tech_dispatch,headers_tech)) = yes;
res(tech)$sum( (n,tech_dispatch,headers_tech), technology_data_upload(n,tech,'res',tech_dispatch,headers_tech)) = yes;



***** Parameters *****

*--- Generation technologies ---*
technology_data(n,tech,headers_tech) = sum((tech_res_con,tech_dispatch), technology_data_upload(n,tech,tech_res_con,tech_dispatch,headers_tech)) ;
eta(n,tech) = technology_data(n,tech,'eta_con') ;
carbon_content(n,tech) = technology_data(n,tech,'carbon_content') ;
kappa_fix(n,tech) = technology_data(n,tech,'fixed_costs') ;
CO2price(n,tech) = technology_data(n,tech,'CO2_price') ;

kappa_inv_overnight(n,tech) = technology_data(n,tech,'oc') ;
lifetime(n,tech) = technology_data(n,tech,'lifetime') ;
interest_rate(n,tech) = technology_data(n,tech,'interest_rate') ;
nu_p(n,tech) = technology_data(n,tech,'max_installable') ;
nu_e(n,tech) = technology_data(n,tech,'max_energy') ;
fuelprice(n,tech) = technology_data(n,tech,'fuel costs') ;


*--- Storage technologies ---*
kappa_v_sto_in(n,sto) = storage_data(n,sto,'mc_in');
kappa_v_sto_out(n,sto) = storage_data(n,sto,'mc_out');
eta_sto_in(n,sto) = storage_data(n,sto,'efficiency_in');
eta_sto_out(n,sto) = storage_data(n,sto,'efficiency_out');
kappa_fix_sto_p_in(n,sto) = storage_data(n,sto,'fixed_costs_power_in');
kappa_fix_sto_p_out(n,sto) = storage_data(n,sto,'fixed_costs_power_out');
kappa_fix_sto_e(n,sto) = storage_data(n,sto,'fixed_costs_energy');
phi_sto_ini(n,sto) = storage_data(n,sto,'level_start');
tau_sto(n,sto) = storage_data(n,sto,'etop_max') ;

kappa_inv_overnight_sto_e(n,sto) = storage_data(n,sto,'oc_energy');
kappa_inv_overnight_sto_p_in(n,sto) = storage_data(n,sto,'oc_capacity_in');
kappa_inv_overnight_sto_p_out(n,sto) = storage_data(n,sto,'oc_capacity_out');
lifetime(n,sto) = storage_data(n,sto,'lifetime');
interest_rate(n,sto) = storage_data(n,sto,'interest_rate');
nu_sto_e(n,sto) = storage_data(n,sto,'max_energy');
nu_sto_p(n,sto) = storage_data(n,sto,'max_power');


*--- Temporal data ---*
delta_y(n,year,h) = time_data_upload(h,n,year,'demand')  ;
phi_res_y(n,year,res,h) = sum(headers_time$(sameas(res,headers_time)), time_data_upload(h,n,year,headers_time));



***************  CALCULATE DERIVED PARAMETERS  *********************************

kappa_v(n,tech) = fuelprice(n,tech)/eta(n,tech) + carbon_content(n,tech)/eta(n,tech)*CO2price(n,tech)  ;

kappa_i(n,tech) = kappa_inv_overnight(n,tech)*( interest_rate(n,tech) * (1+interest_rate(n,tech))**(lifetime(n,tech)) )
                  / ( (1+interest_rate(n,tech))**(lifetime(n,tech))-1 )       ;

kappa_i_res(n,res) = kappa_i(n,res) ;
kappa_fix_res(n,res) = kappa_fix(n,res) ;

kappa_i_sto_e(n,sto) = kappa_inv_overnight_sto_e(n,sto)*( interest_rate(n,sto) * (1+interest_rate(n,sto))**(lifetime(n,sto)) )
                 / ( (1+interest_rate(n,sto))**(lifetime(n,sto))-1 )       ;

kappa_i_sto_p_in(n,sto) = kappa_inv_overnight_sto_p_in(n,sto)*( interest_rate(n,sto) * (1+interest_rate(n,sto))**(lifetime(n,sto)) )
                 / ( (1+interest_rate(n,sto))**(lifetime(n,sto))-1 )       ;

kappa_i_sto_p_out(n,sto) = kappa_inv_overnight_sto_p_out(n,sto)*( interest_rate(n,sto) * (1+interest_rate(n,sto))**(lifetime(n,sto)) )
                 / ( (1+interest_rate(n,sto))**(lifetime(n,sto))-1 )       ;



*************** Sensitivity with electrification (power-to-X) ******************

Parameter
delta_x          Annual demand for generic electrification
nu_x             Maximum hourly demand for electrification
;

Positive variable
D_X(n,h)         Hourly demand for electrification
;







